package com.ezhuan.system.dao.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.ezhuan.common.dao.impl.BaseDaoImpl;
import com.ezhuan.common.util.PageInfoUtil;
import com.ezhuan.system.dao.IASuggestDao;
import com.ezhuan.system.pojo.ASuggest;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 用户表Dao的实现类
 * 
 * @author wangshSxt
 *
 */
public class ASuggestDaoImpl extends BaseDaoImpl<ASuggest> implements IASuggestDao
{
	private ASuggest suggest = new ASuggest();
	
	@Override
	public ASuggest findOneDao(Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ suggest.getTableName() +" where 1 = 1 ");
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			/* 按照ID查询 */
			sqlSb.append(" and id = ? ");
			paramsList.add(condMap.get("id"));
		}else
		{
			/* 木有任何条件,直接返回Null */
			return null ; 
		}
		Record record = Db.findFirst(sqlSb.toString(), paramsList.toArray());
		if (record != null)
		{
			/* 用户信息 */
			ASuggest withdraw = new ASuggest();
			return withdraw.parseRecord(record);
		}
		return null;
	}

	@Override
	public List<ASuggest> findCondListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		/* 拼装SQL语句 */
		StringBuffer sqlSb = new StringBuffer();
		List<Object> paramsList = new ArrayList<Object>();
		/* 拼装查询条件 */
		sqlSb.append("select * from "+ suggest.getTableName() +" where 1 = 1 ");
		/* 查看徒弟 parentId */
		if (condMap.get("usersId") != null && !"".equalsIgnoreCase(condMap.get("usersId") + ""))
		{
			sqlSb.append(" and usersId = ? ");
			paramsList.add(condMap.get("usersId"));
		}
		
		/* 排序 */
		if("createTimeDesc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			sqlSb.append(" order by createTime desc ");
		}else
		{
			sqlSb.append(" order by pubTime desc ");
		}
		
		List<Record> recordList = Collections.EMPTY_LIST ; 
		/* 分页 */
		if (pageInfoUtil != null)
		{
			recordList = this.findByPage(pageInfoUtil, sqlSb.toString(), paramsList);
		}else
		{
			recordList = this.find(sqlSb.toString(), paramsList);
		}

		/* 存储结果 */
		List<ASuggest> suggestList = new ArrayList<ASuggest>();
		for (Iterator iterator = recordList.iterator(); iterator.hasNext();)
		{
			Record record = (Record) iterator.next();
			/* 用户信息 */
			ASuggest suggest = new ASuggest();
			suggest = suggest.parseRecord(record);
			suggestList.add(suggest);
		}
		return suggestList;
	}
}
